package com.nf.health.app.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.WebViewActivity;
import com.nf.health.app.customview.HeadListView.HeaderAdapter;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.customview.SwipeView.OnSwipeStatusChangeListener;
import com.nf.health.app.http.Apis;
import com.nf.health.app.models.RegimenContent;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.List;

public class News2Adapter extends BaseAdapter implements SectionIndexer, HeaderAdapter, OnScrollListener{
	List<RegimenContent> newsList;
	Activity activity;
	LayoutInflater inflater = null;
	
	private ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();
	
	public News2Adapter(Activity activity, List<RegimenContent> newsList) {
		this.activity = activity;
		this.newsList = newsList;
		inflater = LayoutInflater.from(activity);
	}
	@Override
	public int getCount() {
		return newsList == null ? 0 : newsList.size();
	}

	@Override
	public Object getItem(int position) {
		if (newsList != null && newsList.size() != 0) {
			return newsList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		RegimenContent item = newsList.get(position);
		ViewHolder holder = ViewHolder.get(parent, convertView, R.layout.item_regimen_content2, position);
		ImageView iv_img = holder.getView(R.id.iv_img);
		ImageLoaderUtils.getinstance(activity).getImage(iv_img, item.getImg(), R.drawable.default_image);
		holder.setText(R.id.tv_regimen_title, item.getTitle());
		SwipeView swipeView  = holder.getView(R.id.swipeView);
		View ll_content  = holder.getView(R.id.ll_content);
		View delete  = holder.getView(R.id.delete);
		delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(listener != null)
					listener.onClick(position, "");		
				unClosedSwipeView.clear();
			}
		});
		
		
		ll_content.setTag(position);
		ll_content.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (unClosedSwipeView.size() > 0) {
                    closeAllOpenedSwipeView();
                } else {
                	int position = (Integer) v.getTag();
                	Bundle bundle = new Bundle();
					bundle.putString("httpUrl", Apis.HOST_COMMON+"/store/docdetail.html?articleid="+newsList.get(position).getDocid()+"&userid="+PreferenceHelper.getString(PreferenceHelper.USERID, ""));
					bundle.putString("docid",newsList.get(position).getDocid());//文章是否收藏
					bundle.putString("title",newsList.get(position).getTitle());//文章tite
					bundle.putBoolean("colection",true);//文章是否包含收藏\
					bundle.putString("imgurl",newsList.get(position).getImg());//文章图片
					bundle.putString("content",newsList.get(position).getDescs());//文章副标题
					bundle.putBoolean("isshare",true);
					ActivityUtils.showActivity(activity, WebViewActivity.class, bundle);				
                }
			}
		});
		
		swipeView.setOnSwipeStatusChangeListener(new OnSwipeStatusChangeListener() {
			
			@Override
			public void onSwiping(SwipeView swipingSwipeView) {
				if (!unClosedSwipeView.contains(swipingSwipeView)) {
		            closeAllOpenedSwipeView();
		        }
		        
		        if (!unClosedSwipeView.contains(swipingSwipeView)) {
		            unClosedSwipeView.add(swipingSwipeView);
		        }
		        
			}
			
			@Override
			public void onOpen(SwipeView openedSwipeView) {
				for (int i = 0; i < unClosedSwipeView.size(); i++) {
		            if (unClosedSwipeView.get(i) != openedSwipeView) {
		                unClosedSwipeView.get(i).close();
		            }
		        }
		        if (!unClosedSwipeView.contains(openedSwipeView)) {
		            unClosedSwipeView.add(openedSwipeView);
		        }
			}
			
			@Override
			public void onClose(SwipeView closedSwipeView) {
				unClosedSwipeView.remove(closedSwipeView);
			}
		});
		
		
		if(TextUtils.isEmpty(item.getDescription())){

			holder.setText(R.id.tv_regimen_content, item.getDescs());
		}else{

			holder.setText(R.id.tv_regimen_content, item.getDescription());
		}



		return holder.getConvertView();
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}
	//滑动监听
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
	}

	@Override
	public int getHeaderState(int position) {
		int realPosition = position;
		if (realPosition < 0 || position >= getCount()) {
			return HEADER_GONE;
		}
		int section = getSectionForPosition(realPosition);
		int nextSectionPosition = getPositionForSection(section + 1);
		if (nextSectionPosition != -1
				&& realPosition == nextSectionPosition - 1) {
			return HEADER_PUSHED_UP;
		}
		return HEADER_VISIBLE;
	}
	@Override
	public void configureHeader(View header, int position, int alpha) {
		// TODO Auto-generated method stub

	}
	@Override
	public Object[] getSections() {
		return null;
	}
	@Override
	public int getPositionForSection(int section) {
		return 0;
	}
	@Override
	public int getSectionForPosition(int position) {
		return 0;
	}
	
	private OnMyClickListener listener;
	public void setOnMyClickListener(OnMyClickListener listener){
		this.listener = listener;
	}
	private void closeAllOpenedSwipeView() {
        for (int i = 0; i < unClosedSwipeView.size(); i++) {
            if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
                unClosedSwipeView.get(i).close();
            }
        }
    }
}
